package com.avapir.snake.Actors.NPC;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import com.avapir.snake.Core.Painter;

/**
 * This class describes food pieces and their creating
 * 
 * 
 * @author Alpen Ditrix
 */
public class Food {
	/**
	 * 
	 * @author Alpen Ditrix
	 */
	public enum FoodType {
		VALHALLA, WALL, TYPICAL, BIG, dead, INVULNERABILITY
	}

	/**
	 * Type of this food piece
	 */
	protected FoodType type;

	/**
	 * X position
	 */
	protected int x;

	/**
	 * Y position
	 */
	protected int y;

	/**
	 * Creates food piece at random place with random type
	 */
	public Food() {
		placeFoodAtRandomPosition();
		type = getRandomType();
	}

	/**
	 * Hard manual food injecting.
	 * 
	 * @deprecated Never locally used
	 * @param type
	 * @param x
	 * @param y
	 */
	@Deprecated
	public Food(FoodType type, int x, int y) {
		this.x = x;
		this.y = y;
		this.type = type;
	}

	/**
	 * Soft manual food injecting.
	 * 
	 * @deprecated Never locally used
	 * @param x
	 * @param y
	 */
	@Deprecated
	public Food(int x, int y) {
		this.x = x;
		this.y = y;
	}

	/**
	 * Selects the appropriate paint color and draws object
	 * 
	 * @param g
	 */
	public void draw(Graphics g) {
		Color c = null;
		switch (type) {
		case TYPICAL:
			c = Color.gray;
			break;
		case BIG:
			c = Color.darkGray;
			break;
		case WALL:
			c = Color.gray;
			break;
		case VALHALLA:
			c = Color.yellow;
			break;
		case INVULNERABILITY:
			c = Color.green;
			break;
		}
		g.setColor(c);
		if (type == FoodType.VALHALLA || type == FoodType.dead) {
			g.fillRoundRect(x, y, Painter.getCellSize(), Painter.getCellSize(),
					Painter.getCellSize(), Painter.getCellSize());
		}
		if (type == FoodType.WALL) {
			g.fillRect(x, y, Painter.getCellSize(), Painter.getCellSize());
		} else {
			g.drawRoundRect(x, y, Painter.getCellSize(), Painter.getCellSize(),
					Painter.getCellSize(), Painter.getCellSize());
		}
	}

	/**
	 * Is used for AI targeting, scoring and buffing
	 * 
	 * @return type of this food piece
	 */
	public FoodType getType() {
		return type;
	}

	/**
	 * Is used for AI targeting and lunching
	 * 
	 * @return X position
	 */
	public int getX() {
		return x;
	}

	/**
	 * Is used for AI targeting and lunching
	 * 
	 * @return Y position
	 */
	public int getY() {
		return y;
	}

	@Override
	public String toString() {
		return type.toString();
	}

	/**
	 * OMFG!! A LITTLE BIT OF KOREAN RANDOM DETECTED HERE!
	 * 
	 * @return
	 */
	private FoodType getRandomType() {
		Random r = new Random();
		int res = r.nextInt(10000);
		if (res < 50)
			return FoodType.VALHALLA;
		else if (res < 200)
			return FoodType.WALL;
		else if (res < 3500)
			return FoodType.BIG;
		else if (res < 3575)
			return FoodType.INVULNERABILITY;
		else
			return FoodType.TYPICAL;
	}

	/**
	 * RTFMethodName
	 */
	private void placeFoodAtRandomPosition() {
		Random r = new Random();
		int rr = r.nextInt(Painter.getWidth());
		x = rr - rr % Painter.getCellSize();
		rr = r.nextInt(Painter.getHeight() - 30) + 30;
		y = rr - rr % Painter.getCellSize();
	}
}